//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// ChartBarView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "ChartBarView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartBarView

IMPLEMENT_DYNCREATE(CChartBarView, CBCGPChartExampleView)

CChartBarView::CChartBarView()
	: CBCGPChartExampleView(CChartBarView::IDD)
{
	//{{AFX_DATA_INIT(CChartBarView)
	m_nFillGradientType = 0;
	m_bShowDataLabels = TRUE;
	m_nDataLabelPosition = 0;
	m_bDisplayShadow = TRUE;
	//}}AFX_DATA_INIT
}

CChartBarView::~CChartBarView()
{
}

void CChartBarView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartBarView)
	DDX_Control(pDX, IDC_SERIES_SHADOW, m_wndDisplayShadow);
	DDX_Control(pDX, IDC_DATA_LABEL_POSITION, m_wndDataLabelPosition);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_CBIndex(pDX, IDC_FILL_GRADIENT_TYPE, m_nFillGradientType);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_POSITION, m_nDataLabelPosition);
	DDX_Check(pDX, IDC_SERIES_SHADOW, m_bDisplayShadow);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartBarView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartBarView)
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_POSITION, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_FILL_GRADIENT_TYPE, OnUpdateChart)
	ON_BN_CLICKED(IDC_SERIES_SHADOW, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartBarView diagnostics

#ifdef _DEBUG
void CChartBarView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartBarView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartBarView message handlers

void CChartBarView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	BCGPChartCategory category = 
		(m_Feature == CDemoFeature::BCGP_StackedBar || m_Feature == CDemoFeature::BCGP_StackedBar100 || 
		m_Feature == CDemoFeature::BCGP_ClusteredBar) ? 
			BCGPChartBar : 
			BCGPChartColumn;

	BCGPChartType type = 
		(m_Feature == CDemoFeature::BCGP_StackedColumn2D || m_Feature == CDemoFeature::BCGP_StackedBar || m_Feature == CDemoFeature::BCGP_StackedColumnSideBySide2D) ? BCGP_CT_STACKED :
		(m_Feature == CDemoFeature::BCGP_StackedColumn1002D || m_Feature == CDemoFeature::BCGP_StackedBar100) ? 
		BCGP_CT_100STACKED : 
		BCGP_CT_SIMPLE;

	pChart->SetChartType(category, type);

	pChart->SetChartTitle(_T("Vehicle Expenses"));

	CBCGPChartBarSeries* pBarSeries1 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Fuel")));
	CBCGPChartBarSeries* pBarSeries2 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Taxes")));
	CBCGPChartBarSeries* pBarSeries3 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Maintenance")));
	CBCGPChartBarSeries* pBarSeries4 = DYNAMIC_DOWNCAST(CBCGPChartBarSeries, pChart->CreateSeries(_T("Other")));

	pBarSeries1->AddDataPoint(m_arYears[4], 1);
	pBarSeries1->AddDataPoint(m_arYears[3], 2);
	pBarSeries1->AddDataPoint(m_arYears[2], 3);
	pBarSeries1->AddDataPoint(m_arYears[1], 4);
	pBarSeries1->AddDataPoint(m_arYears[0], 14);

	pBarSeries2->AddDataPoint(10);
	pBarSeries2->AddDataPoint(12);
	pBarSeries2->AddDataPoint(15);
	pBarSeries2->AddDataPoint(17);
	pBarSeries2->AddDataPoint(12);

	pBarSeries3->AddDataPoint(5);
	pBarSeries3->AddDataPoint(7);
	pBarSeries3->AddDataPoint(11);
	pBarSeries3->AddDataPoint(14);
	pBarSeries3->AddDataPoint(19);

	pBarSeries4->AddDataPoint(2);
	pBarSeries4->AddDataPoint(3);
	pBarSeries4->AddDataPoint(5);
	pBarSeries4->AddDataPoint(3);
	pBarSeries4->AddDataPoint(2);

	if (m_Feature == CDemoFeature::BCGP_StackedColumnSideBySide2D)
	{
		pBarSeries1->SetGroupID(0);
		pBarSeries2->SetGroupID(0);

		pBarSeries3->SetGroupID(1);
		pBarSeries4->SetGroupID(1);
	}

	if (m_wndChart.GetGraphicsManager()->GetType() == CBCGPGraphicsManager::BCGP_GRAPHICS_MANAGER_GDI)
	{
		m_wndDisplayShadow.EnableWindow(FALSE);
		m_wndDisplayShadow.ShowWindow(SW_HIDE);
		
		m_bDisplayShadow = FALSE;
	}

	OnUpdateChart();
}

void CChartBarView::OnUpdateChart() 
{
	UpdateData();

	m_wndDataLabelPosition.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataLabels(m_bShowDataLabels);

	if (m_bShowDataLabels)
	{
		BCGPChartDataLabelOptions dataLabelOptions = pChart->GetDataLabelOptions();
		dataLabelOptions.m_position = (BCGPChartDataLabelOptions::LabelPosition)m_nDataLabelPosition;

		pChart->SetDataLabelsOptions(dataLabelOptions);
	}

	CBCGPBrush::BCGP_GRADIENT_TYPE type;

	switch (m_nFillGradientType)
	{
	case 0:
	default:
		type = (CBCGPBrush::BCGP_GRADIENT_TYPE)-1;
		break;

	case 1:
		type = CBCGPBrush::BCGP_NO_GRADIENT;
		break;

	case 2:
		type = CBCGPBrush::BCGP_GRADIENT_HORIZONTAL;
		break;

	case 3:
		type = CBCGPBrush::BCGP_GRADIENT_VERTICAL;
		break;

	case 4:
		type = CBCGPBrush::BCGP_GRADIENT_BEVEL;
		break;

	case 5:
		type = pChart->GetChartCategory() == BCGPChartBar ? 
			CBCGPBrush::BCGP_GRADIENT_PIPE_HORIZONTAL : CBCGPBrush::BCGP_GRADIENT_PIPE_VERTICAL;
		break;
	}

	for (int i = 0; i < pChart->GetSeriesCount(); i++)
	{
		CBCGPChartSeries* pSeries = pChart->GetSeries(i);
		ASSERT_VALID(pSeries);

		pSeries->SetDefaultFillGradientType(type);
	}

	pChart->SetSeriesShadow(m_bDisplayShadow);
	
	pChart->Redraw();
}
